package com.mofang.activity.login;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.mofang.R;
import com.mofang.activity.BaseTitleViewActiviey;
import com.mofang.activity.MofangCommonUpdateView;
import com.mofang.activity.MyTabHostActivity;
import com.mofang.controller.UserController;
import com.mofang.utils.ViewHelper;
import com.pactera.framework.exception.IException;
import com.pactera.framework.util.ToastHelper;

public class LoginActivity extends BaseTitleViewActiviey {

	private EditText edit_login_userName;
	private EditText edit_login_password;
	// private TextView txt_forgot_password;
	private Button btn_login;
	private Button btn_register;
	private Button btn_forgot_password;
	private UserController userController;

	ViewHelper viewHelper;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_login);
		super.onCreate(savedInstanceState);
		setNavigationTitle("用户登陆");
		setNavigationLeftBtnImage(R.drawable.return_arrow);
		userController = new UserController();
		viewHelper = new ViewHelper(mContext);
	}

	@Override
	protected void initView() {
		super.initView();
		btn_forgot_password = (Button) findViewById(R.id.btn_forgot_password);
		edit_login_userName = (EditText) findViewById(R.id.edit_login_username);
		edit_login_password = (EditText) findViewById(R.id.edit_login_password);
		btn_login = (Button) findViewById(R.id.btn_login);
		btn_register = (Button) findViewById(R.id.btn_login_register);
	}

	@Override
	protected void setListener() {
		super.setListener();
		btn_login.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// if (validateInput()) {
				// 登陆代码
				String userName = edit_login_userName.getText().toString();
				String password = edit_login_password.getText().toString();
				userController.loginFromRemote(new LoginUpdateView(
						LoginActivity.this), userName, password);

				// }
			}
		});

		btn_register.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent registerIntent = new Intent();
				registerIntent.setClass(LoginActivity.this,
						RegisterActivity.class);
				startActivity(registerIntent);
			}
		});
		btn_forgot_password.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				final Intent userIntent = new Intent();
				userIntent.setClass(mContext, FindPasswordActivity.class);
				startActivity(userIntent);
			}
		});
	}

	private boolean validateInput() {
		String userName = edit_login_userName.getText().toString();
		String password = edit_login_password.getText().toString();

		if (TextUtils.isEmpty(userName)) {
			edit_login_userName.requestFocus();
			ToastHelper.getInstance().showLongMsg(
					getString(R.string.login_account_not_null));
			return false;
		}
		if (TextUtils.isEmpty(password)) {
			edit_login_password.requestFocus();
			ToastHelper.getInstance().showLongMsg(
					getString(R.string.login_password_not_null));
			return false;
		}
		return true;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.login, menu);
		return true;
	}

	public void addIntentLink(final TextView view, final String text,
			Class clazz) {

		SpannableString sp = new SpannableString(text);

		final Intent userIntent = new Intent();

		userIntent.setClass(mContext, clazz);

		sp.setSpan(new IntentSpan(new OnClickListener() {

			public void onClick(View view) {
				mContext.startActivity(userIntent);
			}

		}), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		view.setText(sp);
		view.setMovementMethod(LinkMovementMethod.getInstance());
	}

	// IntentSpan 类
	public class IntentSpan extends ClickableSpan {

		private final OnClickListener listener;

		public IntentSpan(View.OnClickListener listener) {
			this.listener = listener;
		}

		@Override
		public void onClick(View view) {
			listener.onClick(view);
		}
	}

	private class LoginUpdateView extends MofangCommonUpdateView<Boolean> {
		private ProgressDialog progressDialog;

		public LoginUpdateView(Context context) {
			super(context);
		}

		@Override
		public void onPreExecute() {
			super.onPreExecute();
			progressDialog = ProgressDialog.show(LoginActivity.this, null,
					getResources().getString(R.string.handle_in_process), true, false);
		}

		@Override
		public void onPostExecute(Boolean result) {
			//result=false;
			progressDialog.dismiss();
			if (result) {
				Intent intent = new Intent(mContext, MyTabHostActivity.class);
				startActivity(intent);
				finish();
			} else {
				viewHelper.showBTNDialog(getResources().getString(R.string.txt_login_failed));
			}
		}

		@Override
		public void handleException(IException ex) {
			viewHelper.showBTNDialog(ex.getMessage());
		}
	}
}
